package engine

import (
	"gateway/core"
)

type routeChain struct {
	handlers []handlerRuntime
	outbound outboundRuntime
}

func newRouteChain(handlers []handlerRuntime, outbound outboundRuntime) core.Chain {
	return &routeChain{
		handlers: handlers,
		outbound: outbound,
	}
}

func (this *routeChain) Dispatch(msg core.Message) bool {
	go this.doChain(msg)
	return true
}

func (this *routeChain) doChain(msg core.Message) {
	for _, h := range this.handlers {
		rs := h.handler.Handler(msg)
		if !rs {
			return
		}
	}
	this.outbound.outbound.Send(msg)
}
